package com.tr.syn;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author TRasdzz
 * @version 1.0
 * @since create in 2021/3/11 21:46
 */
public class SafeBuyTicket {
    public static void main(String[] args) {
        Ticket buyTicket = new Ticket();
        new Thread(buyTicket, "小红").start();
        new Thread(buyTicket, "小绿").start();
        new Thread(buyTicket, "小蓝").start();

    }
}

class Ticket implements Runnable {
    int ticketNum = 1000;
    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (ticketNum <= 0) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() + ticketNum--);
            } finally {
                lock.unlock();
            }
        }
    }
}
